<template>
	<view class="cont_msg">
		<view class="nav_title">
			<van-icon @click="$daiyueapp.tab(4)" name="arrow-left" style="margin-left: 20rpx;" />
			<van-search shape="round" use-left-icon-slot="true" style="width: 530rpx;" v-model="keyword"
				left-icon="https://daiyue.gzzhenyi.net/static/image/store/search@4x.png" @search="search"
				placeholder="请输入搜索关键词" />
		</view>
		<van-tabs tab-active-class="font-weight:bold;" :line-width="0" :line-height="0" title-active-color="#000"
			:active="stateIndex" swipeable :animated="true" :swipeable="true" @change="tabBar">
			<van-tab :title="item" v-for="(item, i) in titleList " :key="i">
				<view class="big_box">
					<view class="card_li" v-for="(e, index) in orderList" :key="e.id" @click.stop="toOrder(e.id)">
						<view class="card_top">
							<view class="left_top">
								<text>{{ e.shop.shopname }}</text>
								<van-icon name="arrow" color="#999" />
							</view>
							<view class="right_top">
								{{ states[e.state] }}
							</view>
						</view>
						<view class="contant_msg" v-for="(ites, index) in e.goods " :key="ites.id">
							<image class="img_msg" :src="baseUrl + ites.image" mode="aspectFill">
							</image>
							<view class="msg_right">
								<view class="msg_li">
									<text class="title">{{ ites.title }}</text>
									<text class="price">￥{{ ites.market_price }}</text>
								</view>
								<view class="msg_nums">
									<text class="title">{{ ites.status_text }}</text>
									<text>x{{ ites.number }}</text>
								</view>
								<view class="pay">
									实付款：￥{{ ites.price }}
								</view>
							</view>
						</view>
						<view class="btn_list" v-if="e.state == 4">
							<view class="btn_lft" @tap.stop="toAppraise(e)">
								评价
							</view>
							<view class="btn_lft">
								售后客服
							</view>
							<view class="btn_lfo">
								再来一单
							</view>
						</view>
						<view class="btn_list" v-if="e.state == 1">
							<view class="btn_lft" @tap.stop="toCancal(e.id)">
								取消订单
							</view>
							<view class="btn_lfo" @tap.stop="toPay(e.id)">
								去支付
							</view>
						</view>
						<view class="btn_list" v-if="e.state == 2">
							<view class="btn_lft" @tap.stop="toCancal(e.id)">
								取消订单
							</view>
						</view>
						<view class="btn_list" v-if="e.state == 3">
							<view class="btn_lft" @tap.stop="toCancal(e.id)">
								取消订单
							</view>
							<view class="btn_lfo" @tap.stop="confirm(e.id)">
								确认收货
							</view>
						</view>
					</view>
					<van-empty description="暂无订单" v-if="orderList.length == 0" />

				</view>

			</van-tab>
		</van-tabs>
		<van-popup round :show="show" @close="onClose">
			<view class="center">
				<text>该操作无法恢复，确定取消吗？</text>
				<view class="btn">
					<view class="btn_left" @click="onClose">
						取消
					</view>
					<view class="btn_right" @click="delectOn">
						确定
					</view>
				</view>
			</view>
		</van-popup>
	</view>
</template>

<script>
import { getOrderList, cancelOrder, confirmOrder, payOrder } from '@/api/api.js'
import baseUrl from '@/api/baseUrl';
export default {
	data() {
		return {
			baseUrl,
			show: false,
			index: 0,
			id: "",
			// 数据
			states: ["", "待支付", "待发货", "待收货", "待评论", "售后订单", "已完成", "已取消"],//订单状态
			titleList: ["全部", "待付款", "待发货", "待收货", "待评价"],
			orderList: [],
			stateIndex: 0,
			keyword: ''
		}
	},
	onLoad(options) {
		this.stateIndex = Number(options.state)
		
	},
	onShow() {
		this.order()
	},
	methods: {
		order() {
			getOrderList({
				state: this.stateIndex,
				keyword: this.keyword || ''
			}).then(res => {
				console.log(res)
				if (res.code == 1) {
					this.orderList = res.data.data
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			})
		},
		search(e) {
			this.keyword = e.detail
			this.order()
		},
		tabBar(e) {
			this.stateIndex = e.detail.index
			this.order()
		},
		toCancal(id) {
			this.show = true
			this.id = id
		},

		onClose() {
			this.show = false
		},
		// 取消订单
		delectOn() {
			cancelOrder({ order_id: this.id }).then(res => {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.show = false
				if (res.code == 1) {
					this.order()
				}
			})
		},
		//确认订单
		confirm(id) {
			confirmOrder({ order_id: id }).then(res => {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				if (res.code == 1) {
					this.order()
				}
			})
		},

		toDelet(i) {
			this.orderList.splice(i, 1)
		},
		clickPay() {

		},
		toPay(id) {
			payOrder({
				order_id: id
			}).then(res => {
				if (res.code == 1) {
					uni.requestPayment({
						provider: 'wxpay',
						"timeStamp": res.data.timeStamp, //时间戳
						"nonceStr": res.data.nonceStr,//随机字符串
						"package": res.data.package,//接口返回的prepay_id
						"signType": res.data.signType,//签名算法，需要与后台下单时一致
						"paySign": res.data.paySign,// 签名

						success: function (res) {
							console.log('success:' + JSON.stringify(res));
							uni.navigateTo({ url: '/long/payment/payment?payShow=true' })
						},
						fail: function (err) {
							console.log('fail:' + JSON.stringify(err));
							uni.navigateTo({ url: '/long/payment/payment?payShow=false' })
						}
					});
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			})
		},

		toAppraise(e) {
			uni.navigateTo({
				url: `/pages/order/appraise/appraise?id=${e.id}`
			})
		},
		toOrder(id) {
			uni.navigateTo({
				url: `/long/orderDetails/orderDetails?id=${id}`
			})
		}
	}
}
</script>

<style lang="scss">
.center {
	padding: 84rpx 56rpx 38rpx 56rpx;
	border-radius: 24rpx;

	text {
		display: inline-block;
		width: 600rpx;
		text-align: center;
		font-size: 34rpx;
		color: #333333;
	}

	.btn {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 50rpx;

		.btn_left {
			width: 264rpx;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 30rpx;
			color: #FF6200;
			border: 2rpx solid #FF6200;
			margin-right: 20rpx;
			border-radius: 12rpx;
		}

		.btn_right {
			width: 264rpx;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 30rpx;
			color: #fff;
			background-color: #FF6200;
			border: 2rpx solid #FF6200;
			border-radius: 12rpx;
		}
	}
}

.nav_title {
	width: 100%;
	display: flex;
	align-items: center;
	padding-top: 80rpx;
	background-color: #fff;

}

.big_box {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding-bottom: 100rpx;
}

.card_li {
	background-color: #fff;
	padding: 20rpx;
	width: 670rpx;
	margin-top: 20rpx;
	border-radius: 30rpx;

	.btn_list {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		margin-top: 58rpx;

		.btn_lft {
			width: 150rpx;
			height: 54rpx;
			border-radius: 35rpx;
			opacity: 1;
			background: #FFFFFF;
			box-sizing: border-box;
			border: 2rpx solid #333333;
			font-size: 28rpx;
			color: #333333;
			// font-weight: bold;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-right: 20rpx;
		}

		.btn_lfo {
			width: 150rpx;
			height: 54rpx;
			border-radius: 35px;
			opacity: 1;
			background: #FFFFFF;
			box-sizing: border-box;
			border: 2rpx solid #FF6200;
			font-size: 28rpx;
			color: #FF6200;
			// font-weight: bold;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-right: 20rpx;
		}

	}

	.contant_msg {
		display: flex;
		margin-top: 20rpx;

		.img_msg {
			width: 196rpx;
			height: 146rpx;
			border-radius: 8rpx;
		}

		// align-items: center;
		.msg_right {
			margin-right: 20rpx;
			width: 434rpx;

			.msg_li {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-left: 20rpx;

				.title {
					font-size: 30rpx;
					// font-weight: bold;
					color: #333333;
				}

				.price {
					font-size: 26rpx;
					font-weight: normal;
					// line-height: 27rpx;
					color: #333333;
				}
			}

			.msg_nums {
				margin-top: 20rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-left: 20rpx;

				text {
					font-size: 22rpx;
					font-weight: normal;
					line-height: 20rpx;
					color: #5C5C5C;
				}
			}

			.pay {
				font-size: 30rpx;
				font-weight: normal;
				line-height: 28rpx;
				text-align: right;
				color: #333333;
				margin-top: 40rpx;
			}
		}
	}

	.card_top {
		display: flex;
		align-items: center;
		justify-content: space-between;

		.left_top {
			text {
				font-size: 30rpx;
				// font-weight: bold;
				line-height: 30rpx;
				margin-right: 10rpx;
				color: #333333;
			}
		}

		.right_top {
			font-size: 28rpx;
			font-weight: normal;
			line-height: 30rpx;
			text-align: right;
			color: #FF6200;
		}
	}
}

.cont_msg {
	.van-tabs ::v-deep .van-tab--active {
		font-weight: bold;
	}
}
</style>